package com.cike.productmanager;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cike.productmanager.util.DataApplication;

public class LoginActivity extends Activity {

	private EditText txtUserName=null;
	private EditText txtPassWord=null;
	
	private Button loginbtn;
	
	//TODO 以后加入忘记密码和注册功能要使用以下按键
	private Button forgetPwd;			
	private Button registerbtn;
	
	private String username="";
	private String password="";
	
	public static String USERNAME="";
	
	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor editor;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);
        
        //如果曾经登陆过，就提取sharedPreferences文件中存下来的账号和密码
        sharedPreferences = this.getSharedPreferences("Login",MODE_PRIVATE);
        editor = sharedPreferences.edit();
        username = sharedPreferences.getString("user", null);
        password = sharedPreferences.getString("pwd", null);
        
        
        
        
        txtUserName = (EditText)findViewById(R.id.login_user_edit);
        txtPassWord = (EditText)findViewById(R.id.login_passwd_edit);
        loginbtn = (Button)findViewById(R.id.login_login_btn);
        forgetPwd= (Button)findViewById(R.id.forget_passwd);
        registerbtn = (Button)findViewById(R.id.register);
        
        
        
        
        
        if(username!=null){
        	txtUserName.setText(username);
        }
        if(password!=null){
        	txtPassWord.setText(password);
        }
        
        
        
        
        
        
        
        
        loginbtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				//获取填写的用户名密码
				username = txtUserName.getText().toString();
				password = txtPassWord.getText().toString();
				
				
				
				
				//TODO 做登陆检查，暂时没有写出来，使用true测试其他模块
				if(true){
					//存储用户登陆成功的账号
					editor.putString("user", username);
					editor.putString("pwd", password);
					// 一定要提交  
	                editor.commit();
					
	                
	                
	                //TODO 登陆成功以后，将从服务器获取这几个信息，现在服务器没写好，暂时设这个代替
	                DataApplication.currentUser.setIMEI("123456789012345");
	                DataApplication.currentUser.setUserID(101);
	                DataApplication.currentUser.setLevel(1);
	                
	                
	                
	                //从SQLite数据库提取所有已有信息
	                DataApplication.loadListfromDB();
	                
	                
	                
	                
	                //转入主界面
					Intent intent=new Intent(getApplicationContext(),MainPad.class);
	        		startActivity(intent);
	        		Toast.makeText(getApplicationContext(),"登录成功!", 2).show();
				}
			}
		});
        
        
        
    }
    
    
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.welcome, menu);
        return true;
    }

    
    
    


}